﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using ShortcutToolbox.Core;

namespace ShortcutToolbox.View
{
    public partial class AboutForm : Form, ILangConnector
    {
        static AboutForm _instance = null;
        static object _locker = new object();

        public static AboutForm Instance
        {
            get
            {
                if(_instance == null)
                {
                    lock(_locker)
                    {
                        if (_instance == null)
                            _instance = new AboutForm();
                    }
                }
                return _instance;
            }
        }

        public AboutForm()
        {
            InitializeComponent();
            I18N.Changed += OnLangeChanged;
            OnLangeChanged(this, new LanguageChangedEventArgs(I18N.Current));
        }

        public void OnLangeChanged(object sender, LanguageChangedEventArgs e)
        {
            dynamic dylang = e.Value;
            Text = dylang.AboutForm.Title;
            ProgramNameLabel.Text = $"{dylang.AppName} {VERSION.CurrentVersion}";
            AuthorLabel.Text = dylang.AboutForm.AuthorLabel;
            HomePageLabel.Text = dylang.AboutForm.HomePageLabel;
            OKButton.Text = dylang.AboutForm.OKButton;
        }

        protected override void OnClosed(EventArgs e)
        {
            I18N.Changed -= OnLangeChanged;
            base.OnClosed(e);
            _instance = null;
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AuthorLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("mailto: gang.zhuo@gmail.com");
        }

        private void HomePageLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(HomePageLinkLabel.Text);
        }
    }
}
